package cn.wgx.common.security.aop;

import cn.wgx.common.base.BaseEntity;
import cn.wgx.common.security.client.config.entity.LoginAppUser;
import cn.wgx.common.security.util.SysUserUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Date;

/**
 * aop所有service,增强add* update*方法
 * 所有新增实体对象方法添加创建人
 * 所有修改方法添加修改人
 */
@Aspect
@Component
public class ExtensionService {

    //新增方法添加创建人
    @Pointcut("execution(* cn.wgx.common.base.IBaseService+.save*(..)) || "+
              "execution(* com.baomidou.mybatisplus.extension.service.impl.ServiceImpl+.save*(..))||" +
            "execution(* com.baomidou.mybatisplus.extension.service.IService+.save*(..))")
    public void createUserId() {}
    //修改方法设置创建人
    @Pointcut("execution(* cn.wgx.common.base.IBaseService+.update*(..))|| "+
            "execution(* com.baomidou.mybatisplus.extension.service.impl.ServiceImpl+.update*(..)) ||" +
    "execution(* com.baomidou.mybatisplus.extension.service.IService+.update*(..))")
    public void updateUserId() {}

    //所有新增实体对象方法添加创建人
    @Before("createUserId()")
    public void addCreateUserId(JoinPoint joinPoint) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
        if(loginAppUser == null){
            return;
        }
        Object[] args = joinPoint.getArgs();
        for(Object o: args){
            if(o instanceof BaseEntity){
                //单条新增
                BaseEntity baseEntity = (BaseEntity)o;
                baseEntity.setCreateUserId(loginAppUser.getUser_id());
                baseEntity.setUpdateUserId(loginAppUser.getUser_id());
            }else if(o instanceof Collection){
                //批量新增
                for(Object o1:(Collection)o){
                    if(o1 instanceof BaseEntity){
                        BaseEntity baseEntity = (BaseEntity)o1;
                        baseEntity.setCreateUserId(loginAppUser.getUser_id());
                        baseEntity.setUpdateUserId(loginAppUser.getUser_id());
                    }
                }
            }
        }
    }

    //所有修改方法添加修改人
    @Before("updateUserId()")
    public void addUpdateUserId(JoinPoint joinPoint) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
        if(loginAppUser == null){
            return;
        }
        Object[] args = joinPoint.getArgs();
        for(Object o: args){
            if(o instanceof BaseEntity){
                //单条修改
                BaseEntity baseEntity = (BaseEntity)o;
                baseEntity.setUpdateUserId(loginAppUser.getUser_id());
                baseEntity.setUpdateDate(new Date());
                baseEntity.setCreateUserId(null);
                baseEntity.setCreateDate(null);
            }else if(o instanceof Collection){
                //批量修改
                for(Object o1:(Collection)o){
                    if(o1 instanceof BaseEntity){
                        BaseEntity baseEntity = (BaseEntity)o1;
                        baseEntity.setUpdateUserId(loginAppUser.getUser_id());
                        baseEntity.setUpdateDate(new Date());
                        baseEntity.setCreateUserId(null);
                        baseEntity.setCreateDate(null);
                    }
                }
            }
        }
    }
}
